//
//  YJBKlinePictureView.m
//  YunJiaBaoFirst
//
//  Created by 腾云小白龍 on 16/7/31.
//  Copyright © 2016年 腾云小白龍. All rights reserved.
//

#import "YJBKlinePictureView.h"

@interface YJBKlinePictureView()
@property (strong, nonatomic) YKTimeDataset* set;
@property (strong, nonatomic) YKLineDataSet*kLineDataSet;

@end

@implementation YJBKlinePictureView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)initView{
    self.backgroundColor = [UIColor colorWithRed:27/255.0 green:24/255.0 blue:32/255.0 alpha:1.0];
    
    UIView *contentView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , 64)];
    contentView.layer.borderColor = [UIColor grayColor].CGColor;
    contentView.layer.borderWidth = .5;
    [self addSubview:contentView];
    
    UILabel *label = ({
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 90, 44)];
        label.backgroundColor = kColorWithRGB(219, 216, 218);
        label.text = @"价格走势";
        label.textColor = [UIColor darkGrayColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18];
        label;
    });
    [contentView addSubview:label];
    
    
    UIButton * hourButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label.frame), 0, (SCREEN_WIDTH - 90) / 2, 44)];
    [hourButton setTitle:@"分时图" forState:UIControlStateNormal];
    [hourButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [hourButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    hourButton.titleLabel.font = [UIFont systemFontOfSize:18];
    hourButton.backgroundColor = MAIN_COLOR;
    self.hourButton = hourButton;
    
    UIButton * dayButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(hourButton.frame), 0, (SCREEN_WIDTH - 90) / 2, 44)];
    [dayButton setTitle:@"K线图" forState:UIControlStateNormal];
    [dayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [dayButton setTitleColor:DEFAULT_TEXT_COLOR forState:UIControlStateNormal];
    dayButton.titleLabel.font = [UIFont systemFontOfSize:12];
    dayButton.backgroundColor =  kColorWithRGB(253, 184, 75);
    self.dayButton = dayButton;
    
    [contentView addSubview:hourButton];
    [contentView addSubview:dayButton];

     self.imageView = ({
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(dayButton.frame), SCREEN_WIDTH, 20)];
        imageView.image = kImage(@"timeLine_sel");
         imageView.backgroundColor = DEFAULT_COLOR;
        [contentView addSubview:imageView];
        imageView;
    });
    
 
#pragma mark -- 分时图
    self.timeLineView = [[YKTimeLineView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(contentView.frame), SCREEN_WIDTH, SCREEN_HEIGHT - 64 -85 -20 -50)];
    [self addSubview:self.timeLineView];
    [self.timeLineView setupChartOffsetWithLeft:50 - 12  top:10 right:15 bottom:10]; //曲线图位置
    self.timeLineView.gridBackgroundColor = [UIColor colorWithRed:27/255.0 green:24/255.0 blue:32/255.0 alpha:1.0];//曲线图背景颜色
    self.timeLineView.borderColor = [UIColor colorWithRed:203/255.0 green:215/255.0 blue:224/255.0 alpha:1.0];
    self.timeLineView.borderWidth = .3;
    self.timeLineView.uperChartHeightScale = 0.7;
    self.timeLineView.xAxisHeitht = 25;         //手数柱形图高度;
    self.timeLineView.countOfTimes = 185 + 175;//242; // 控制曲线图x族画的宽度
    self.timeLineView.endPointShowEnabled = YES;//显示结束点

    YKTimeDataset * set  = [[YKTimeDataset alloc]init];
    self.set = set;
    //        set.data = timeArray;
    set.avgLineCorlor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:8/255.0 alpha:1.0];
    set.priceLineCorlor = [UIColor colorWithRed:24/255.0 green:96/255.0 blue:254/255.0 alpha:1.0];//价格曲线颜色
    set.lineWidth = 1.2f;
    set.highlightLineWidth = .8f; //按压线
    set.highlightLineColor = [UIColor colorWithRed:60/255.0 green:76/255.0 blue:109/255.0 alpha:1.0];
    //柱形图的颜色
    set.volumeTieColor = [UIColor grayColor];
    set.volumeRiseColor = [UIColor colorWithRed:233/255.0 green:47/255.0 blue:68/255.0 alpha:1.0];
    set.volumeFallColor = [UIColor colorWithRed:33/255.0 green:179/255.0 blue:77/255.0 alpha:1.0];
    //曲线图下的阴影颜色
    set.fillStartColor = [UIColor colorWithRed:24/255.0 green:96/255.0 blue:254/255.0 alpha:1.0];
    //        set.fillStartColor = [UIColor colorWithRed:87/255.0 green:96/255.0 blue:142/255.0 alpha:1.0];
    
    //        set.fillStopColor = [UIColor whiteColor];
    set.fillStopColor = [UIColor colorWithRed:87/255.0 green:96/255.0 blue:142/255.0 alpha:1.0];
    
    set.fillAlpha = .2f;
    set.drawFilledEnabled = YES;
    self.timeLineView.delegate = self;
    self.timeLineView.highlightLineShowEnabled = YES;
    [self.timeLineView setupData:set];
    

#pragma mark--k线图
    self.klineView = [[YKLineChartView alloc] initWithFrame:CGRectMake(0,  CGRectGetMaxY(contentView.frame), SCREEN_WIDTH, SCREEN_HEIGHT - 64 -85 -20 -50)];
    [self addSubview:self.klineView];
    YKLineDataSet * dataset = [[YKLineDataSet alloc]init];
    self.kLineDataSet = dataset;
    //        dataset.data = array;
    dataset.highlightLineColor = [UIColor colorWithRed:60/255.0 green:76/255.0 blue:109/255.0 alpha:1.0];
    dataset.highlightLineWidth = 0.7;
    dataset.candleRiseColor = [UIColor colorWithRed:233/255.0 green:47/255.0 blue:68/255.0 alpha:1.0];
    dataset.candleFallColor = [UIColor colorWithRed:33/255.0 green:179/255.0 blue:77/255.0 alpha:1.0];
    dataset.avgLineWidth = 1.f;
    dataset.avgMA10Color = [UIColor colorWithRed:252/255.0 green:85/255.0 blue:198/255.0 alpha:1.0];
    dataset.avgMA5Color = [UIColor colorWithRed:67/255.0 green:85/255.0 blue:109/255.0 alpha:1.0];
    dataset.avgMA20Color = [UIColor colorWithRed:216/255.0 green:192/255.0 blue:44/255.0 alpha:1.0];
    dataset.candleTopBottmLineWidth = 1;
    
    [self.klineView setupChartOffsetWithLeft:50 -12 top:10 right:10 bottom:10];
    self.klineView.gridBackgroundColor = [UIColor colorWithRed:27/255.0 green:24/255.0 blue:32/255.0 alpha:1.0];
    self.klineView.borderColor = [UIColor colorWithRed:203/255.0 green:215/255.0 blue:224/255.0 alpha:1.0];
    self.klineView.borderWidth = .3;
    self.klineView.candleWidth = 8;
    self.klineView.candleMaxWidth = 30;
    self.klineView.candleMinWidth = 1;
    self.klineView.uperChartHeightScale = 0.7;
    self.klineView.xAxisHeitht = 25;
    self.klineView.delegate = self;
    self.klineView.highlightLineShowEnabled = YES;
    self.klineView.zoomEnabled = YES;
    self.klineView.scrollEnabled = YES;
    [self.klineView setupData:dataset];
    self.klineView.hidden = YES;
    
    
}

//分时图
-(void)setKTimeLineMinArr:(NSMutableArray *)KTimeLineMinArr{
    _KTimeLineMinArr = KTimeLineMinArr;
    NSMutableArray * timeArray = [NSMutableArray array];
    for (NSDictionary * dic in KTimeLineMinArr) {
        YKTimeLineEntity * e = [[YKTimeLineEntity alloc]init];
        
        //时间戳转时间
        float dateTime = [dic[@"time"] floatValue]/1000;
        NSDate * date = [NSDate dateWithTimeIntervalSince1970:dateTime];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString * startDate = [dateFormatter stringFromDate:date]; //@"2016-05-20";//[NSString stringWithFormat:@"%@",[balanceFrame.accountBalanceModel.time floatValue]];
        
        CGFloat rise = [dic[@"rise"] floatValue];
        if (rise > 0) {
            e.rate = [NSString stringWithFormat:@"%+.2f%%",rise * 100];
        }else{
            e.rate = [NSString stringWithFormat:@"%.2f%%",rise * 100];
            
        }
        
        
        e.currtTime = [startDate substringWithRange:NSMakeRange(10, 6)];
        e.preClosePx = self.openPrice;
        e.avgPirce = [dic[@"average"] doubleValue];
        e.lastPirce = [dic[@"price"]doubleValue];
        e.volume = [dic[@"volume"]doubleValue];
        
        
        [timeArray addObject:e];
    }
    self.set.data = timeArray;
    [self.timeLineView setNeedsDisplay];// 画线

}

//k线图
-(void)setKLineDayArr:(NSMutableArray *)KLineDayArr{
    NSMutableArray * array = [NSMutableArray array];
    for (NSDictionary * dic in KLineDayArr) {
        
        YKLineEntity * entity = [[YKLineEntity alloc]init];
        
        //时间戳转时间
        float dateTime = [dic[@"time"] floatValue]/1000;
        NSDate * date = [NSDate dateWithTimeIntervalSince1970:dateTime];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString * startDate = [dateFormatter stringFromDate:date];
        
        entity.date = [startDate substringWithRange:NSMakeRange(0, 11)];
        entity.high = [dic[@"highest"] doubleValue];
        entity.open = [dic[@"open"] doubleValue];
        
        entity.low = [dic[@"lowest"] doubleValue];
        entity.close = [dic[@"close"] doubleValue];
        
        entity.ma5 = [dic[@"daily"] doubleValue];
        entity.ma10 = [dic[@"monthly"] doubleValue];
        
        entity.ma20 = [dic[@"yearly"] doubleValue];
        entity.volume = [dic[@"volume"] doubleValue];
        [array addObject:entity];
        
    }
    self.kLineDataSet.data = array;
    
    [self.klineView setNeedsDisplay];//执行画线

}

@end
